$(document).ready(function(){
    $("form#signin").submit(function() { // loginForm is submitted
        var correo = $('#username').attr('value'); // get username
        var password = $('#password').attr('value'); // get password

        if (correo && password)
        { // values are not empty
            $.ajax({
                type: "GET",              
                url: "cgis/consultaUsuario.cgi", // URL of the Perl script
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                // send username and password as parameters to the Perl script
                data: "correo=" + correo + "&password=" + password,
                // script call was *not* successful
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    $('div#loginResult').text("responseText: " + XMLHttpRequest.responseText 
                        + ", textStatus: " + textStatus 
                        + ", errorThrown: " +errorThrown);
                    $('div#loginResult').addClass("error");
                }, // error
                // script call was successful
                // data contains the JSON values returned by the Perl script
                success: function(data){
                    if (data.error) { // script returned error
                        $('div#loginResult').text("data.error: "  +data.error);
                        $('div#loginResult').addClass("error");
                    } // if
                    else { // login was successful
                        if(parseInt(data.hits)>0)
                        {
                            createCookie('k',data.correo+"#"+data.nombre,7);
                            location.reload();
                        }
                        else
                        {
                            $('div#loginResult').text("Email o contraseña incorrectos");
                            $('div#loginResult').css("color","red");
                        }
                    //            $('form#loginForm').hide();
                    //            $('div#loginResult').text("data.success: " + data.success 
                    //              + ", data.userid: " + data.userid);
                    //            $('div#loginResult').addClass("success");
                    } //else
                } // success
            }); // ajax
        } // if
        else
        {
            $('div#loginResult').text("enter username and password");
            $('div#loginResult').addClass("error");
        } // else
        $('div#loginResult').fadeIn();
        return false;
    });
    
    

    
    
    
    
    $(".signin").click(function(e) {          
        e.preventDefault();
        $("fieldset#signin_menu").toggle();
        $(".signin").toggleClass("menu-open");
    });
                
    $("fieldset#signin_menu").mouseup(function() {
        return false
    });
    $(document).mouseup(function(e) {
        if($(e.target).parent("a.signin").length==0) {
            $(".signin").removeClass("menu-open");
            $("fieldset#signin_menu").hide();
        }
    });





});